package cs3343.project.family;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class EventCardFrame {
	
	EventCardFrame() { }
	
	EventCardFrame(final Card card, final Character character) {
		
		//JOptionPane.showMessageDialog(null, card.description, "Message", 1);
		
		final JFrame cardFrame = new JFrame(card.getClass().getSimpleName());
		//final JPanel imgPanel = new JPanel();
		final JPanel descriptionPanel = new JPanel();
		final JPanel cardPanel = new JPanel();
		final JButton okay = new JButton("OK");
		//final JLabel cardimg = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "card.jpg"));
		
		//String temp = "<html><body>Hello<br>Pig</body></html>";
		//final JButton A = new JButton(temp);
		final JLabel description = new JLabel(card.description);
		final JButton A = new JButton(card.getOption(0));
		final JButton B = new JButton(card.getOption(1));
		final JButton C = new JButton(card.getOption(2));
		final JButton D = new JButton(card.getOption(3));
		final JLabel msg = new JLabel(" ");
			
		String[] option = ToolCardFrame.availableCards(character, "EventCard");
		JComboBox availableCard = new JComboBox(option);
		
		Container contentPane = cardFrame.getContentPane();
	    Font f = new Font("Dialog", Font.PLAIN, 30);
	    msg.setFont(f);
	    //description.setFont(new Font("Dialog", Font.PLAIN, 20));
	    
	    descriptionPanel.setLayout(new BorderLayout());
		cardPanel.setLayout(new GridLayout(3, 2));
		//imgPanel.setLayout(new GridLayout(1, 1));
		
		//imgPanel.add(cardimg);
		//cardFrame.add(imgPanel);
		cardFrame.add(cardPanel, BorderLayout.SOUTH);
		cardFrame.add(descriptionPanel, BorderLayout.NORTH);

		descriptionPanel.add(description, BorderLayout.NORTH);
		descriptionPanel.add(availableCard, BorderLayout.SOUTH);
		cardPanel.add(A);
		cardPanel.add(B);
		cardPanel.add(C);
		cardPanel.add(D);
		cardPanel.add(msg);
		//cardFrame.add(cardimg);
		//cardimg.setVisible(false);
		cardPanel.add(okay);
		okay.setEnabled(false);
		
		contentPane.add(cardPanel);
		cardFrame.setVisible(true);
		cardFrame.show();
		cardFrame.setSize(800, 600);
		
		A.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        	//cardimg.setVisible(true);
	        	A.setEnabled(false);
	        	B.setVisible(false);
	        	C.setVisible(false);
	        	D.setVisible(false);
	        	okay.setEnabled(true);
	        	msg.setText("Your Self-Esteem: " + card.getResult(0));
	        	/*System.out.println(Start.character.length);
	        	for (int i = 1; i < Start.character.length; i++) {
	        		System.out.println(String.valueOf(Integer.parseInt(card.getResult(0))));
	        		Start.character[i].esteem += Integer.parseInt(card.getResult(0));
	        		System.out.println(Start.character[i].name + " " + Start.character[i].esteem);
	        	}*/
	        	int oriEsteem = character.getEsteem();
	        	character.setEsteem(oriEsteem += Integer.parseInt(card.getResult(0)));
	        	for(int i = 0; i < StartGame.selectedPlayer; i++){
	        		InitGame.se[i].setText(String.valueOf(StartGame.character[i].getEsteem()));
	        	}
	        	System.out.println(character.name + " " + character.esteem);
	        }
	    }); 
		
		B.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        	//cardimg.setVisible(true);
	        	B.setEnabled(false);
	        	A.setVisible(false);
	        	C.setVisible(false);
	        	D.setVisible(false);
	        	okay.setEnabled(true);
	        	msg.setText("Your Self-Esteem: " + card.getResult(1));

	        	/*System.out.println(Start.character.length);
	        	for (int i = 1; i < Start.character.length; i++) {
	        		System.out.println(String.valueOf(Integer.parseInt(card.getResult(1))));
	        		Start.character[i].esteem += Integer.parseInt(card.getResult(1));
	        		System.out.println(Start.character[i].name + " " + Start.character[i].esteem);
	        	}*/
	        	int oriEsteem = character.getEsteem();
	        	character.setEsteem(oriEsteem += Integer.parseInt(card.getResult(1)));
	        	for(int i = 0; i < StartGame.selectedPlayer; i++){
	        		InitGame.se[i].setText(String.valueOf(StartGame.character[i].getEsteem()));
	        	}
	        	System.out.println(character.name + " " + character.esteem);
	        }
	    }); 
		
		C.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        	//cardimg.setVisible(true);
	        	C.setEnabled(false);
	        	A.setVisible(false);
	        	B.setVisible(false);
	        	D.setVisible(false);
	        	okay.setEnabled(true);
	        	msg.setText("Your Self-Esteem: " + card.getResult(2));
	        	/*System.out.println(Start.character.length);
	        	for (int i = 1; i < Start.character.length; i++) {
	        		System.out.println(String.valueOf(Integer.parseInt(card.getResult(2))));
	        		Start.character[i].esteem += Integer.parseInt(card.getResult(2));
	        		System.out.println(Start.character[i].name + " " + Start.character[i].esteem);
	        	}*/
	        	int oriEsteem = character.getEsteem();
	        	character.setEsteem(oriEsteem += Integer.parseInt(card.getResult(2)));
	        	for(int i = 0; i < StartGame.selectedPlayer; i++){
	        		InitGame.se[i].setText(String.valueOf(StartGame.character[i].getEsteem()));
	        	}
	        	System.out.println(character.name + " " + character.esteem);
	        }
	    }); 
		
		D.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e){
	        	//cardimg.setVisible(true);
	        	D.setEnabled(false);
	        	A.setVisible(false);
	        	B.setVisible(false);
	        	C.setVisible(false);
	        	okay.setEnabled(true);
	        	msg.setText("Your Self-Esteem: " + card.getResult(3));
	        	/*System.out.println(Start.character.length);
	        	for (int i = 1; i < Start.character.length; i++) {
	        		System.out.println(String.valueOf(Integer.parseInt(card.getResult(3))));
	        		Start.character[i].esteem += Integer.parseInt(card.getResult(3));
	        		System.out.println(Start.character[i].name + " " + Start.character[i].esteem);
	        	}*/
	        	int oriEsteem = character.getEsteem();
	        	character.setEsteem(oriEsteem += Integer.parseInt(card.getResult(3)));
	        	for(int i = 0; i < StartGame.selectedPlayer; i++){
	        		InitGame.se[i].setText(String.valueOf(StartGame.character[i].getEsteem()));
	        	}
	        	System.out.println(character.name + " " + character.esteem);
	        }
	    }); 
		
		okay.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e){
	        	cardFrame.dispose();
	        }
	    }); 
	}
}
